package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.iot.domain.bo.IotElevatorFaultBo;
import org.dromara.iot.domain.vo.IotElevatorFaultVo;
import org.dromara.iot.service.IIotElevatorFaultService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电梯故障
 *
 * @author lsl
 * @date 2024-06-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/elevatorFault")
public class IotElevatorFaultController extends BaseController {

    private final IIotElevatorFaultService iotElevatorFaultService;

    /**
     * 查询电梯故障列表
     */
    @SaCheckPermission("iot:elevatorFault:list")
    @GetMapping("/list")
    public TableDataInfo<IotElevatorFaultVo> list(IotElevatorFaultBo bo, PageQuery pageQuery) {
        return iotElevatorFaultService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯故障列表
     */
    @SaCheckPermission("iot:elevatorFault:export")
    @Log(title = "电梯故障", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotElevatorFaultBo bo, HttpServletResponse response) {
        List<IotElevatorFaultVo> list = iotElevatorFaultService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯故障", IotElevatorFaultVo.class, response);
    }

    /**
     * 获取电梯故障详细信息
     *
     * @param faultId 主键
     */
    @SaCheckPermission("iot:elevatorFault:query")
    @GetMapping("/{faultId}")
    public R<IotElevatorFaultVo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable String faultId) {
        return R.ok(iotElevatorFaultService.queryById(faultId));
    }

    /**
     * 新增电梯故障
     */
    @SaCheckPermission("iot:elevatorFault:add")
    @Log(title = "电梯故障", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotElevatorFaultBo bo) {
        return toAjax(iotElevatorFaultService.insertByBo(bo));
    }

    /**
     * 修改电梯故障
     */
    @SaCheckPermission("iot:elevatorFault:edit")
    @Log(title = "电梯故障", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotElevatorFaultBo bo) {
        return toAjax(iotElevatorFaultService.updateByBo(bo));
    }

    /**
     * 删除电梯故障
     *
     * @param faultIds 主键串
     */
    @SaCheckPermission("iot:elevatorFault:remove")
    @Log(title = "电梯故障", businessType = BusinessType.DELETE)
    @DeleteMapping("/{faultIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] faultIds) {
        return toAjax(iotElevatorFaultService.deleteWithValidByIds(List.of(faultIds), true));
    }
}
